import type { ReactNode } from 'react';
import type { WrappedFormUtils, GetFieldDecoratorOptions } from 'antd/lib/form/Form.d';
import type { Schema as FormSchema } from 'gantd/lib/schema-form';
import type { OperatorType } from './operators';

export type OrderType = 'ASC' | 'DESC';

// searchform schema
export type SearchFormSchema = Record<string, FormSchema>;

// 分页信息
export interface PageInfo {
  beginIndex: number;
  pageSize: number;
}

// 基础字段
export interface BaseField {
  fieldName: string;
  title: string;
}

// 基础可查询字段
export interface BaseSupportFilterField extends BaseField {
  componentType?: string;
  props?: any;
  options?: GetFieldDecoratorOptions; // 表单验证的配置
  dependencies?: string[]; // 字段依赖数组
  onDependenciesChange?: (value: any[], props: any, form: WrappedFormUtils) => any; // 依赖字段值变化的回调
}

// 可查询字段
export interface SupportFilterField extends BaseSupportFilterField {
  suppOperator?: OperatorType[];
  type?: 'string' | 'number' | 'date' | 'boolean' | 'codelist' | 'object';
  operator?: OperatorType;
  required?: boolean;
}

// 可排序字段
export type SupportOrderField = BaseField;

// 基础搜索字段
export interface BaseSearchField {
  fieldName: string;
}

// 搜索字段
export interface SearchField extends BaseSearchField {
  operator?: OperatorType;
  [key: string]: any;
}

// 排序字段
export interface OrderField {
  fieldName: string;
  orderType: OrderType;
}

// UI配置
export interface UiConfigProps {
  searchType?: 'auto' | 'click' | 'autoNoDelay';
  autoSearchWait?: number;
  searchAfterReset?: boolean;
}

// 普通渲染配置
export interface PanelConfig {
  searchFields: SearchField[];
  orderFields?: OrderField[];
  uiConfig?: UiConfigProps;
}

// 兼容模式渲染配置
export interface CompatibilityModePanelConfig {
  searchFields: SearchField[];
  uiConfig?: UiConfigProps;
}

// 基础视图
export interface BaseView {
  viewId: string; // 唯一标识
  name: string;
  version: string;
  isSystem?: boolean;
  viewType?: 'custom' | 'company'; // 视图类型 custom: 自定义视图 company: 公司视图
}

// 普通视图
export interface View extends BaseView {
  panelConfig: PanelConfig;
}

// 兼容模式视图
export interface CompatibilityModeView extends BaseView {
  panelConfig: CompatibilityModePanelConfig;
}

// 基础系统筛选器字段
export interface BaseSystemFilterItem {
  fieldName: string;
  value: any;
  operator?: OperatorType;
}

// 系统筛选器字段
export interface SystemFilterItem extends BaseSystemFilterItem {
  operator: OperatorType;
}

// 基础系统筛选器
export interface BaseSystemFilter {
  dataName: string;
  relateViewId: string;
  relateViewVersion: string;
  pageInfo?: PageInfo;
}

// 普通系统筛选器
export interface SystemFilter extends BaseSystemFilter {
  filterItems: SystemFilterItem[];
}

// 兼容模式筛选器
export interface CompatibilityModelSystemFilter extends BaseSystemFilter {
  filterItems: BaseSystemFilterItem[];
}

// SmartSearch查询数据结构
export interface SearchData {
  whereList: { fieldName: string; operator: OperatorType; value: any }[];
  orderList: { fieldName: string; orderType: OrderType }[];
  pageInfo: PageInfo | undefined;
}

// 传统查询数据结构
export interface FilterData {
  filterInfo: Record<string, any>;
  orderInfo: Record<string, any>;
  pageInfo: PageInfo | undefined;
}

// 本地筛选器
export interface LocalFilter {
  dataName: string;
  filterItems: SystemFilterItem[] | Object;
  pageInfo?: PageInfo;
  isLast?: boolean; // 是否是上次查询
  isAutoSearch?: boolean; // 自动查询
}

// SmartSearch本地存筛选器
export type SmartSearchLocalFilter = Record<string, LocalFilter[]>;

// 自定义组件
export interface CustomComponent {
  name: string; // 组件名
  component: ReactNode; // 自定义组件
  multiple?: 'mode' | 'multiple'; // 组件配置多选的字段
}

// 高级查询回调
export type OnSmartSearch = (
  params: SearchData | FilterData,
  isInit: boolean,
  filterParams: SystemFilterItem[],
) => void;

export type Size = {
  width: number;
  height: number;
};

export type Ref = {
  search: () => void;
  reset: () => void;
  validate: () => Promise<boolean>;
  getFormNode: () => HTMLElement;
  getFormRef: () => WrappedFormUtils;
};
